<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Crypt;


class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    public function register(Request $request)
    {
        if ($request->ps == "email") {
            $user = User::where('email', $request->email)->first();
            if ($user) {
                $id = Crypt::encrypt($user->id);
                $code = Crypt::encrypt(time() + 60 * 60);
                $data = ['email' => $user->email, 'name' => $user->name, 'id' => $id, 'code' => $code];
                Mail::send('auth.test', $data, function ($message) use ($data) {
                    $message->to($data['email'], $data['name'])->subject("欢迎注册 Ca King Shop 账号,请激活您的账号！");
                });
//        $this->guard()->login($user);

//        return $this->registered($request, $user)
//            ?: redirect($this->redirectPath());
                return redirect(route('mail'))->with('success', '邮件已发送，请及时激活,超过一个小时失效');
            } else {
                return redirect(route('mail'))->with('error', '非常抱歉，您的邮箱还未注册');
            }
        } else {
            $this->validator($request->all())->validate();

            event(new Registered($user = $this->create($request->all())));


            $id = Crypt::encrypt($user->id);
            $code = Crypt::encrypt(time() + 60 * 60);
            $data = ['email' => $request->email, 'name' => $request->name, 'id' => $id, 'code' => $code];
            Mail::send('auth.test', $data, function ($message) use ($data) {
                $message->to($data['email'], $data['name'])->subject("欢迎注册 Ca King Shop 账号,请激活您的账号！");
            });
//        $this->guard()->login($user);

//        return $this->registered($request, $user)
//            ?: redirect($this->redirectPath());
            return redirect(route('register'))->with('success', '注册成功，邮件已发送，请及时激活,超过一个小时失效');
        }
    }
    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
//    protected $redirectTo = '/admin';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:10|unique:users',
            'email' => 'required|string|email|max:255|unique:users',
            'phone' => 'required|regex:/^1[34578][0-9]{9}$/|unique:users',
            'password' => 'required|string|min:5|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);
    }
}
